{% extends 'car_system/base.html' %}

{% block title %}汽车数据列表 - 汽车数据分析系统{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="fas fa-car text-primary me-2"></i> 汽车数据列表</h2>
    {% if user.is_superuser %}
    <a href="{% url 'add_car' %}" class="btn btn-success">
        <i class="fas fa-plus-circle me-1"></i> 添加新车型
    </a>
    {% endif %}
</div>

<!-- 筛选表单 -->
<div class="card mb-4">
    <div class="card-header bg-primary text-white">
        <h5 class="mb-0"><i class="fas fa-filter me-2"></i>筛选条件</h5>
    </div>
    <div class="card-body">
        <form method="get" class="row g-3">
            <div class="col-md-3">
                <label for="brand" class="form-label">品牌</label>
                <select name="brand" id="brand" class="form-select">
                    <option value="">所有品牌</option>
                    {% for brand in brands %}
                    <option value="{{ brand }}" {% if brand == selected_brand %}selected{% endif %}>{{ brand }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="col-md-3">
                <label for="min_price" class="form-label">最低价格（万元）</label>
                <input type="number" class="form-control" id="min_price" name="min_price" value="{{ min_price }}" min="0" step="0.1">
            </div>
            <div class="col-md-3">
                <label for="max_price" class="form-label">最高价格（万元）</label>
                <input type="number" class="form-control" id="max_price" name="max_price" value="{{ max_price }}" min="0" step="0.1">
            </div>
            <div class="col-md-3">
                <label for="sort_by" class="form-label">排序方式</label>
                <select name="sort_by" id="sort_by" class="form-select">
                    <option value="ranks" {% if sort_by == 'ranks' %}selected{% endif %}>排名（升序）</option>
                    <option value="-ranks" {% if sort_by == '-ranks' %}selected{% endif %}>排名（降序）</option>
                    <option value="min_price" {% if sort_by == 'min_price' %}selected{% endif %}>价格（从低到高）</option>
                    <option value="-min_price" {% if sort_by == '-min_price' %}selected{% endif %}>价格（从高到低）</option>
                    <option value="-count" {% if sort_by == '-count' %}selected{% endif %}>销量（从高到低）</option>
                    <option value="count" {% if sort_by == 'count' %}selected{% endif %}>销量（从低到高）</option>
                </select>
            </div>
            <div class="col-12">
                <button type="submit" class="btn btn-primary">
                    <i class="fas fa-search me-1"></i> 搜索
                </button>
                <a href="{% url 'car_list' %}" class="btn btn-outline-secondary">
                    <i class="fas fa-redo me-1"></i> 重置
                </a>
            </div>
        </form>
    </div>
</div>

<!-- 数据列表 -->
<div class="card">
    <div class="card-header bg-primary text-white">
        <h5 class="mb-0"><i class="fas fa-list me-2"></i>汽车列表</h5>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead class="table-light">
                    <tr>
                        <th>排名</th>
                        <th>图片</th>
                        <th>车型</th>
                        <th>品牌</th>
                        <th>价格区间</th>
                        <th>月销量</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for car in cars %}
                    <tr>
                        <td>{{ car.ranks }}</td>
                        <td>
                            {% if car.image %}
                            <img src="{{ car.image }}" alt="{{ car.series_name }}" style="width: 80px; height: 60px; object-fit: cover;" class="rounded">
                            {% else %}
                            <div class="bg-secondary text-white rounded d-flex align-items-center justify-content-center" style="width: 80px; height: 60px;">无图片</div>
                            {% endif %}
                        </td>
                        <td>{{ car.series_name }}</td>
                        <td>{{ car.brand_name }}</td>
                        <td>{{ car.price }}</td>
                        <td>{{ car.count }}</td>
                        <td>
                            <div class="btn-group">
                                <a href="{% url 'car_detail' car.series_id %}" class="btn btn-sm btn-outline-primary">
                                    <i class="fas fa-info-circle"></i> 详情
                                </a>
                                {% if user.is_superuser %}
                                <a href="{% url 'edit_car' car.series_id %}" class="btn btn-sm btn-outline-secondary">
                                    <i class="fas fa-edit"></i> 编辑
                                </a>
                                <a href="{% url 'delete_car' car.series_id %}" class="btn btn-sm btn-outline-danger">
                                    <i class="fas fa-trash"></i> 删除
                                </a>
                                {% endif %}
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="7" class="text-center">没有找到符合条件的车型</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>
{% endblock %} 